package com.wing.bill.service;

import com.wing.bill.controller.admin.ReportUtil;
import com.wing.bill.dao.SpecReportDao;
import com.wing.common.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class SpecReportServiceImpl implements SpecReportService{
    @Autowired
    private SpecReportDao specReportDao;

    @Override
    public JsonResult list(String param, String code) {
        HashMap hm = new HashMap();
        hm.put("title","物料领新统计");
        hm.put("data",specReportDao.list(ReportUtil.hundredDays().get(0),ReportUtil.hundredDays().get(1),code,param));
        return JsonResult.success(hm);
    }

    @Override
    public JsonResult topCell(String start, String end, Integer click, String mainCabCode, String code) {
        LocalDateTime startTime = LocalDateTime.parse(start, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        LocalDateTime endTime = LocalDateTime.parse(end, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        if (click==0){//显示格子数据
            HashMap map1 = new HashMap(){{put("title","领新总规格数(个)");put("sign","1");}};
            HashMap map2 = new HashMap(){{put("title","未归还规格数(个)");put("sign","2");}};
            HashMap map3 = new HashMap(){{put("title","暂存规格数(个)");put("sign","3");}};
            HashMap map4 = new HashMap(){{put("title","报修规格数(个)");put("sign","4");}};
            HashMap map5 = new HashMap(){{put("title","报废规格数(个)");put("sign","5");}};
            HashMap map6 = new HashMap(){{put("title","报失规格数(个)");put("sign","6");}};
            map1.putAll(specReportDao.queryTopOne(startTime,endTime,mainCabCode,code));
            map2.putAll(specReportDao.queryTopTwo(mainCabCode,code));
            map3.putAll(specReportDao.queryTopThree(startTime,endTime,mainCabCode,code));
            map4.putAll(specReportDao.queryTopFour(startTime,endTime,mainCabCode,code));
            map5.putAll(specReportDao.queryTopFive(startTime,endTime,mainCabCode,code));
            map6.putAll(specReportDao.queryTopSix(startTime,endTime,mainCabCode,code));
            List list = new ArrayList();
            list.add(map1);
            list.add(map2);
            list.add(map3);
            list.add(map4);
            list.add(map5);
            list.add(map6);
            return JsonResult.success("查询成功", list);
        }
        //每个格子点击事件
        if (click==1){
            return JsonResult.success("查询成功", specReportDao.queryTopOneClick(startTime,endTime,mainCabCode,code));
        }
        if (click==2){
            return JsonResult.success("查询成功",specReportDao.queryTopTwoClick(mainCabCode,code));
        }
        if (click==3){
            return JsonResult.success("查询成功",specReportDao.queryTopThreeClick(startTime,endTime,mainCabCode,code));
        }
        if (click==4){
            return JsonResult.success("查询成功",specReportDao.queryTopFourClick(startTime,endTime,mainCabCode,code));
        }
        if (click==5){
            return JsonResult.success("查询成功",specReportDao.queryTopFiveClick(startTime,endTime,mainCabCode,code));
        }
        if (click==6){
            return JsonResult.success("查询成功",specReportDao.queryTopSixClick(startTime,endTime,mainCabCode,code));
        }
        return null;
    }

    @Override
    public JsonResult firstChart(String start, String end, String param, String mainCabCode,String code) {
        LocalDateTime startTime = LocalDateTime.parse(start, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        LocalDateTime endTime = LocalDateTime.parse(end, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        HashMap hm3 = new HashMap();
        hm3.put("title","物料领新占比(按SPU)");
        List list = new ArrayList();
        List list1 = new ArrayList();
        List list2 = new ArrayList();
        HashMap map = new HashMap();
        HashMap map1 = new HashMap();
        list1.addAll(specReportDao.queryFirstChart(startTime,endTime,mainCabCode,code,param));
        map.putAll(specReportDao.queryFirstChartTotal(startTime,endTime,mainCabCode,code));
        map1.put("value",map.get("totalNum"));
        list.add(map);
        hm3.put("totalSum",list);
        hm3.put("data",list1);
        list2.add(map1);
        hm3.put("totalSumOne",list2);
        return JsonResult.success("查询成功",hm3);
    }

    @Override
    public JsonResult secondChart(String start, String end, String param, String mainCabCode, String code, String other) {
        LocalDateTime startTime = LocalDateTime.parse(start, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        LocalDateTime endTime = LocalDateTime.parse(end, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        HashMap hm3 = new HashMap();
        if (other.equals("temp")){
            hm3.put("title","物料暂存占比(按SPU)");
        }else if (other.equals("rep")){
            hm3.put("title","物料报修占比(按SPU)");
        }else if (other.equals("rec")){
            hm3.put("title","物料报废占比(按SPU)");
        }else if (other.equals("rep")){
            hm3.put("title","物料报失占比(按SPU)");
        }
        List list = new ArrayList();
        List list1 = new ArrayList();
        List list2 = new ArrayList();
        HashMap map = new HashMap();
        HashMap map1 = new HashMap();
        list1.addAll(specReportDao.querySecondChart(startTime,endTime,mainCabCode,code,param,other));
        map.putAll(specReportDao.querySecondChartTotal(startTime,endTime,mainCabCode,code,other));
        map1.put("value",map.get("totalNum"));
        list.add(map);
        hm3.put("totalSum",list);
        hm3.put("data",list1);
        list2.add(map1);
        hm3.put("totalSumOne",list2);
        return JsonResult.success("查询成功",hm3);
    }

    @Override
    public JsonResult thirdChart(String param, String mainCabCode, String code) {
        HashMap hm3 = new HashMap();
        hm3.put("title","物料未归还占比(按SPU)");
        List list = new ArrayList();
        List list1 = new ArrayList();
        List list2 = new ArrayList();
        HashMap map = new HashMap();
        HashMap map1 = new HashMap();
        list1.addAll(specReportDao.queryThirdChart(mainCabCode,code,param));
        map.putAll(specReportDao.queryThirdChartTotal(mainCabCode,code));
        map1.put("value",map.get("totalNum"));
        list.add(map);
        hm3.put("totalSum",list);
        hm3.put("data",list1);
        list2.add(map1);
        hm3.put("totalSumOne",list2);
        return JsonResult.success("查询成功",hm3);
    }
}
